package com.efast.cafe.portal.bean.query;

import com.efast.cafe.framework.util.QueryStringUtil;
import com.efast.cafe.framework.util.SqlConstants.OpType;
import com.efast.cafe.util.StringUtil;

/*
 * List数据类型 多选
 * @author Administrator
 *
 */
public class ListFieldType extends AbstractFieldType {

	/**
	 * 
	 */
	private static final long serialVersionUID = -8581155006743199069L;
	
	/**
	 * 处理list数据类型（（key op value 逻辑符 。。））
	 * @param t（value以~分隔）
	 * @return
	 */
	@Override
	public <T extends CommonTableConditionBean> T getHandledObject(QueryParameterBean p,T t) {
		String combineSql = "";
		if (!StringUtil.isEmpty(t.getValue())) {
			String [] arr = t.getValue().split("~");
			String operator = "";
			if (OpType.not_eq.equals(t.getOp())) {
				operator = " NOT IN ";
			} else {
				operator = " IN ";
			}
			if (arr.length > 0) {
				StringBuffer buffer = new StringBuffer();
				String key = t.getKey();
				buffer.append(QueryStringUtil.changeToVarchar(key));
				buffer.append(operator);
				buffer.append("(");
				for (int i = 0; i < arr.length; i++) {
					/*if (OpType.Like.equals(t.getOp())) {
						t.setValue(StringUtil.like(arr[i]));
					}*/
					t.setValue(QueryStringUtil.getSqlDefaultValue(p.getIndex().toString()));
					t.setRealValue(arr[i]);
					t.setOpType(OpType.getOp(t.getOp()));
					t.setKey(QueryStringUtil.changeToVarchar(key));
					contactSql(p,t,key);
					buffer.append(t.getValue());
					if (i < arr.length -1) {
						buffer.append(",");
					}
				}
				buffer.append(")");
				combineSql = buffer.toString();
			} else {
				t.setRealValue(t.getValue());
				t.setValue(QueryStringUtil.getSqlDefaultValue(p.getIndex().toString()));
				combineSql += contactSql(p,t,t.getKey());
			}
		}
		
		t.setCombineSql(combineSql);
		return t;
	}

}
